package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.AliyunUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){

        //根据redis数据库中两个集合的差值来删除垃圾图片
        Jedis jedis = jedisPool.getResource();

        //获得数据库中两个set集合的差值存储到set集合中
        Set<String> set = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        if(set != null && set.size() > 0){
            for (String fileName : set) {

                //删除阿里云上的垃圾图片
                AliyunUtils.deleteFileFromAliyun(fileName);

                //删除redis数据库中的多余数据
                jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
                System.out.println("清理垃圾图片" + fileName);
            }
        }

    }
}
